package com.wwwarehouse.resource_center.adapter.maintenanceunit;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.maintenanceunit.UpdateUnitBean;
import com.wwwarehouse.resource_center.fragment.maintenanceunit.NewUnitFragment;

import java.util.List;

/**
 * Creator by zhenwang.xiang on 2017/10/14 16:31
 * Description: 修改单位的adapter
 */

public class UpdateUnitAdapter extends BaseAdapter {

    private List<UpdateUnitBean> mData;
    private Context mContext;
    private ViewHolder mHolder;
    private GridView mGridView;

    public UpdateUnitAdapter(List<UpdateUnitBean> data, Context context, GridView gridView) {
        this.mData = data;
        this.mContext = context;
        mGridView = gridView;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_gv_update_unit, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ViewGroup.LayoutParams params = finalConvertView.getLayoutParams();
                params.height = (int) (((mGridView.getHeight()) - (mGridView.getVerticalSpacing() * 10)) / 9);
                finalConvertView.setLayoutParams(params);
                mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        mHolder.mBtnSelect.setText(mData.get(position).getUnitName());
        mHolder.mBtnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewUnitFragment newUnitFragment = new NewUnitFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("flag", 1);
                bundle.putSerializable("unit", mData.get(position));
                newUnitFragment.setArguments(bundle);
                ((BaseCardDeskActivity) mContext).pushFragment(newUnitFragment);
            }
        });
        return convertView;
    }

    class ViewHolder {
        Button mBtnSelect;

        public ViewHolder(View view) {
            mBtnSelect = (Button) view.findViewById(R.id.btn_select);
        }
    }
}
